package com.example.activitylifecycleapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends BaseActivity {

    private Button startNormalActivity;
    private Button startDialogActivity;

    private EditText editText;

    private String tag = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(tag,"onCreate");
        setContentView(R.layout.activity_main);
        startDialogActivity = findViewById(R.id.start_dialog_activity);
        startNormalActivity = findViewById(R.id.start_normal_activity);
        editText = findViewById(R.id.edit_text);
        if (savedInstanceState != null){
            editText.setText(savedInstanceState.getString("saveText"));
        }
        startNormalActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        MainActivity.this,NormalActivity.class);
                startActivity(intent);
            }
        });

        startDialogActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(
                        MainActivity.this,DialogActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(tag,"onStart");

    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(tag,"onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(tag,"onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(tag,"onStop");
    }

    @Override
    protected void onDestroy() {
        Log.d(tag,"onDestroy");
        super.onDestroy();

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(tag,"onRestart");
    }


    @Override
    public void onSaveInstanceState(@NonNull Bundle outState, @NonNull PersistableBundle outPersistentState) {
        String saveText = editText.getText().toString();
        if(saveText != null && !saveText.isEmpty()){
            outState.putString("saveText",saveText);
        }
        super.onSaveInstanceState(outState, outPersistentState);

    }
}
